/**
 * 转正规则详情页
 */
import React from 'react';
import { history } from 'umi'
import { Form, Card, Button, Divider, Popconfirm, message } from 'antd';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import pageTabUtil from '@/utils/pageTabUtil';
import authHelper from '@/utils/auth/helper';
// import { BottomAction } from '@/components';
// import { fetchDutyList } from '@/services/components/userform';
import { useRequest, defaultFormatResult } from '@/hooks';
// import { InfoIcon, FormHeader } from '@/molecules';
import { FormHeader } from '@/molecules';
import { fetchCompany } from '@/containers/service';
import { getRule, deleteRule } from './services';
import { BraftTemplate, EvaluationDetail, ResultDetail, AttendanceDetail } from '../containers';
import styles from './index.less';

const { checkCode } = authHelper;

const InformalRuleDetail = ({ location }) => {
    const { query } = location;

    const { data: companyList, loading: companyLoading } = useRequest(
        () =>
            fetchCompany({
                organizationPermissions: 0,
                showTreeFlag: 0
            }),
        { cacheKey: 'companyList', ...defaultFormatResult }
    );

    const { data, loading } = useRequest(() => getRule(query?.id), {
        refreshDeps: [query?.id],
        ready: !!query?.id,
        ...defaultFormatResult
    });

    const { quarterlyPerformanceAssessVO } = data || {};

    // 删除设置
    const { loading: delLoading, run: delRule } = useRequest(deleteRule, {
        manual: true,
        onSuccess: res => {
            const { code, msg } = res;
            if (code === 10000) {
                message.success('操作成功');
                pageTabUtil.goBack();
                return;
            }
            message.error(msg);
        }
    });

    const renderCompanyNames = text => {
        if (data?.common === 1) {
            return '全集团';
        }
        if (text && text.length > 0) {
            const names = text.map(item => companyList?.find(it => it.organizationCode === item)?.organizationName);
            return names.join('、');
        }
        return '--';
    };

    const showBottomAction =
        data &&
        (checkCode('office:turnover:setting:informal:add') ||
            checkCode('office:turnover:setting:informal:edit') ||
            checkCode('office:turnover:setting:informal:delete'));
    return (
        <PageHeaderWrapper>
            <Card
                title="转正规则详情"
                bordered={false}
                loading={loading || companyLoading}
                extra={
                    showBottomAction && (
                        <div className={styles.cardExtra}>
                            {checkCode('office:turnover:setting:informal:delete') && data?.common !== 1 && (
                                <Popconfirm
                                    title="确定删除该规则吗?"
                                    onConfirm={() => delRule(query?.id)}
                                    okText="确定"
                                    cancelText="取消"
                                    placement="bottom"
                                    getPopupContainer={trigger => trigger.parentElement}
                                >
                                    <Button loading={delLoading}>删除</Button>
                                </Popconfirm>
                            )}
                            {checkCode('office:turnover:setting:informal:add') && (
                                <Button
                                    type="primary"
                                    ghost
                                    disabled={delLoading}
                                    onClick={() => {
                                        history.push({
                                            pathname:
                                                '/office/personnelManagement/onboardAndLeave/setting/informal/add',
                                            query: { id: query?.id }
                                        });
                                    }}
                                >
                                    复制
                                </Button>
                            )}
                            {checkCode('office:turnover:setting:informal:edit') && (
                                <Button
                                    type="primary"
                                    disabled={delLoading}
                                    onClick={() => {
                                        history.push({
                                            pathname:
                                                '/office/personnelManagement/onboardAndLeave/setting/informal/edit',
                                            query: { id: query?.id }
                                        });
                                    }}
                                >
                                    编辑
                                </Button>
                            )}
                        </div>
                    )
                }
            >
                <FormHeader title="基本信息" />
                <Form className={styles.formWrap2} style={{ maxWidth: 800 }}>
                    <Form.Item label="规则名称(中文)">{data?.chName || '--'}</Form.Item>
                    <Form.Item label="规则名称(英文)">{data?.enName || '--'}</Form.Item>
                    <Form.Item label="适用行政机构">{renderCompanyNames(data?.companyCodeList)}</Form.Item>
                    {/* <Form.Item label="适用组织机构">{renderOrganizationNames(data?.organizationCodeList)}</Form.Item> */}
                    <Form.Item label="规则简介(中文)">{data?.chIntroduce || '--'}</Form.Item>
                    <Form.Item label="规则简介(英文)">{data?.enIntroduce || '--'}</Form.Item>
                </Form>
                <Divider />
                <FormHeader title="试用期设置" />
                <Form className={styles.formWrap2} style={{ maxWidth: 800 }}>
                    <Form.Item label="全职试用期">以录用申请为准</Form.Item>
                    <Form.Item label="非全职试用期" style={{ marginBottom: 0 }}>
                        {data?.probationPeriodNumber || '--'}
                        {['', '月', '天'][data?.probationPeriodType]}
                    </Form.Item>
                    {/* <Form.Item
                        label={
                            <InfoIcon title="若岗位上有多名员工的时候，系统将自动为新员工分配人事负责人">
                                人事负责人
                            </InfoIcon>
                        }
                    >
                        {dutyList?.find(item => item.dutyCode === data?.guiderHr)?.dutyAlias || '--'}
                    </Form.Item> */}
                </Form>
                <Divider />
                <FormHeader title="转正考评指标" />
                <div style={{ padding: '0 0 24px 0', maxWidth: 1080 }}>
                    <FormHeader title="能力评估：" />
                    <EvaluationDetail data={data} className={styles.formWrap2} />
                    <Divider />
                    <FormHeader title="任务结果：" />
                    <ResultDetail data={data} className={styles.formWrap2} />
                    <Divider />
                    <FormHeader title="季度绩效：" />
                    <Form className={styles.formWrap2}>
                        <Form.Item label="绩效考核要求">
                            综合各季度绩效不低于
                            {quarterlyPerformanceAssessVO?.requirementsLevel || '--'}
                            级则为合格，反之则不合格
                        </Form.Item>
                        <Form.Item label="绩效指标说明">{quarterlyPerformanceAssessVO?.performance || '--'}</Form.Item>
                    </Form>
                    <Divider />
                    <FormHeader title="考勤情况：" />
                    <AttendanceDetail data={data} className={styles.formWrap2} />
                </div>
                <Divider />
                <FormHeader title="转正申请表设置" />
                <Form className={styles.formWrap2}>
                    <Form.Item label="转正申请表">
                        <BraftTemplate prefix="rule" initialValue={data?.applyRuleConfigVO?.content} readOnly />
                    </Form.Item>
                    <Form.Item label="填写规则">
                        {['无需填写', '选填', '必填'][data?.applyRuleConfigVO?.fillInRule] || '--'}
                    </Form.Item>
                </Form>
            </Card>
        </PageHeaderWrapper>
    );
};

export default InformalRuleDetail;
